package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.ViewPagerAdapter;
import com.dingwei.lesyndrome.bean.GamCircleTypeBean;
import com.dingwei.lesyndrome.bean.TabFragmentTitleItem;
import com.dingwei.lesyndrome.mvp.presenter.GamCirclePresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.ui.fragment.GamCircleFragment;
import com.dingwei.lesyndrome.ui.fragment.MyReleaseFragment;
import com.lihao.baselibrary.base.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 社交圈子
 */

public class MyReleaseActivity extends BaseActivity implements Contract.GamCircleView {

    GamCirclePresenter gamCirclePresenter = new GamCirclePresenter(this);

    @Bind(R.id.news_tab_layout)
    TabLayout newsTabLayout;
    @Bind(R.id.m_viewpager)
    ViewPager mViewpager;
    @Bind(R.id.data_layout)
    RelativeLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    @Bind(R.id.img_edit)
    ImageView imgEdit;
    private List<TabFragmentTitleItem> fragmentTitles = new ArrayList<>();

    private List<Fragment> list = new ArrayList<>();
    GamCircleTypeBean bean;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_gam_circle;
    }

    @Override
    protected void initData() {
        gamCirclePresenter.initData();
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void setData(GamCircleTypeBean bean) {
        this.bean = bean;
        for (int i = 0; i < bean.data.size(); i++) {
            TabFragmentTitleItem item = new TabFragmentTitleItem(bean.data.get(i).name, bean.data.get(i).id);
            fragmentTitles.add(item);
            MyReleaseFragment fragment = new MyReleaseFragment();
            Bundle bundle = new Bundle();
            bundle.putString("id", bean.data.get(i).id);
            fragment.setArguments(bundle);
            list.add(fragment);
        }

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.setmFragments(list, fragmentTitles);
        mViewpager.setAdapter(adapter);
        mViewpager.setOffscreenPageLimit(bean.data.size());
        newsTabLayout.setupWithViewPager(mViewpager);

        mViewpager.setCurrentItem(0);
        showData("success");

    }


    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @OnClick({R.id.img_edit, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_edit:
                intent = new Intent(BaseActivity.context,AddCircleActivity.class);
                intent.putExtra("typeBean", (Serializable) this.bean);
                startActivityForResult(intent,1);
                break;
            case R.id.no_net_btn:
                gamCirclePresenter.initData();
                break;
        }
    }

}
